<?php

namespace IDock\Insurance\TPIC;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\TPIC\Request\AddNonCarOrderRequest;
use IDock\Insurance\TPIC\Request\ElecPolicyDownloadRequest;
use IDock\Insurance\TPIC\Request\ElectronicSignRequest;
use IDock\Insurance\TPIC\Request\NonCarProductListRequest;
use IDock\Insurance\TPIC\Request\OfferOrderPriceRequest;
use IDock\Insurance\TPIC\Request\OrderCancelRequest;
use IDock\Insurance\TPIC\Request\OrderStatusQueryRequest;
use IDock\Insurance\TPIC\Request\SaveVehicleRequest;
use IDock\Insurance\TPIC\Request\UnderwritingRequest;
use IDock\Insurance\TPIC\Request\UploadFileRequest;
use IDock\Insurance\TPIC\Request\VehicleModelRequest;
use IDock\Insurance\TPIC\Response\AddNonCarOrderResponse;
use IDock\Insurance\TPIC\Response\CancelOrderResponse;
use IDock\Insurance\TPIC\Response\CommonResponse;
use IDock\Insurance\TPIC\Response\ElecPolicyDownloadResponse;
use IDock\Insurance\TPIC\Response\ElectronicSignResponse;
use IDock\Insurance\TPIC\Response\NonCarProductListResponse;
use IDock\Insurance\TPIC\Response\OfferOrderPriceResponse;
use IDock\Insurance\TPIC\Response\OrderCancelResponse;
use IDock\Insurance\TPIC\Response\OrderStatusQueryResponse;
use IDock\Insurance\TPIC\Response\UnderwritingResponse;
use IDock\Insurance\TPIC\Response\ValidateCarResponse;
use IDock\Insurance\TPIC\Response\VehicleModelResponse;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 太平保险
 */
class TPICConstruct extends Builder implements IDockInterface
{

    protected $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->clientName = $config['clientName'] ?? '';
        $this->publicKeyTP = $config['publicKeyTP'] ?? '';
        $this->privateKey = $config['privateKey'] ?? '';
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @version 4.0
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {

    }

    /**
     * 车辆实际价格接口
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return CommonResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): CommonResponse
    {
        $response = new CommonResponse();
        $response->setCode('200');
        $vehicleActualValuePrinter = new VehicleActualValuePrinter();
        $vehicleActualValuePrinter->actualValue = round($vehicleActualValueVisitor->vehiclePrice * 0.7, 2);
        $response->printer = $vehicleActualValuePrinter;
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response|NonCarProductListResponse|OfferOrderPriceResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        $response = $this->SaveVehicle($premiumInquiryVisitor);
        if (!$response->isSuccess()) {
            return $response;
        }
        $premiumInquiryVisitor->getExtInfoVisitor()->argc['orderNo'] = $response->printer->argc['orderNo'] ?? '';
        $premiumInquiryVisitor->getExtInfoVisitor()->argc['session'] = $response->printer->argc['session'] ?? '';
        $request1 = new OfferOrderPriceRequest();
        $request1->setVisitor($premiumInquiryVisitor);

        /*** @var OfferOrderPriceResponse $response */
        $response = $this->execute($request1);
        $response->printer->argc['orderNo'] = $premiumInquiryVisitor->getExtInfoVisitor()->argc['orderNo'];
        $response->printer->inquiryNo = $premiumInquiryVisitor->getExtInfoVisitor()->argc['orderNo'];
        $response->formatCoverage($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy);
        if ($response->isSuccess()) {
            $proposalStatusVisitor = new ProposalStatusVisitor();
            $proposalStatusVisitor->argc = $response->printer->argc;
            $res = $this->proposalStatus($proposalStatusVisitor);
            if (!empty($res->data)) {
                //自主定价系数
                /**@var PremiumInquiryPrinter $printer */
                $printer = $response->printer;
                foreach ($res->data as $val) {
                    $printer->premiumFactorPrinter->ComFactorAppend($val['profitName'] ?? '', $val['profitCode'] ?? '', intval($val['rate'] ?? '') / 100);
                }
                $response->printer = $printer;
            }
        }
        return $response;
    }

    private function SaveVehicle(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        $request = new SaveVehicleRequest();
        $request->setVisitor($premiumInquiryVisitor);

        $response = $this->execute($request);
        if (!$response->isSuccess()) {
            return $response;
        }
        if (!empty($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList())) {
//            if(!empty($response->printer->argc['orderNo'])){
//                $visitor = new NonCarProductListVisitor();
//                $visitor->seats = $premiumInquiryVisitor->getVehicleVisitor()->vehicleSeat;
//                $visitor->argc['orderNo'] = $response->printer->argc['orderNo'];
//                $noCarRes = $this->nonCarProductList($visitor);
//                /*** @var NonCarProductVisitor $nonCarPrinter */
//                $nonCarPrinter = $premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList()[0] ?? [];
//                if($nonCarPrinter){
//                    $productCode = $nonCarPrinter->productCode;
//                    if(!in_array($productCode,$noCarRes->productCodeList)){
//                        throw new \Exception("请更新驾意险方案");
//                    }
//                }
//            }
            //添加非车订单
            $nonCarVisitor = $premiumInquiryVisitor->getNonCarInquiryVisitor();
            $nonCarVisitor->argc['orderNo'] = $response->printer->argc['orderNo'] ?? '';
            $nonCarResponse = $this->addNonCarOrder($nonCarVisitor);
            if (!$nonCarResponse->isSuccess()) {
                return $nonCarResponse;
            }
        }
        return $response;
    }

    /**
     * @param NonCarInquiryVisitor $nonCarInquiryVisitor
     * @return AddNonCarOrderResponse
     */
    public function addNonCarOrder(NonCarInquiryVisitor $nonCarInquiryVisitor): AddNonCarOrderResponse
    {
        $request = new AddNonCarOrderRequest();
        $request->setVisitor($nonCarInquiryVisitor);

        /**@var AddNonCarOrderResponse $response * */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return UnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): UnderwritingResponse
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /**@var UnderwritingResponse $response * */
        $response = $this->execute($request);

        //非车数据回填重构
//        /*** @var PremiumUnderwritingPrinter $printer */
//        $printer = $response->getPrinter();
//        if ($printer->noCarUnderwritingPrinter->statusCode == 'success' && !empty($premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList())) {
//
//            /*** @var NonCarProductVisitor $nonProduct */
//            $nonProduct = $premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList()[0];
//
//            $nonCarList = $printer->noCarUnderwritingPrinter->getNonCarInquiryList();
//
//            foreach ($nonCarList as &$value) {
//                /*** @var NonCarInquirySinglePrinter $value */
//                $value->productCode = $nonProduct->productCode;
//                $value->planCode = $nonProduct->planCode;
//            }
//        }
//
//        $response->printer = $printer;
        return $response;
    }

    /**
     * 获取支付链接
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return ElectronicSignResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): ElectronicSignResponse
    {
        $request = new ElectronicSignRequest();
        $request->setVisitor($paymentVisitor);

        /**@var ElectronicSignResponse $response * */
        $response = $this->execute($request);

        return $response;
    }

    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): OrderStatusQueryResponse
    {
        $request = new OrderStatusQueryRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var OrderStatusQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): OrderCancelResponse
    {
        $request = new OrderCancelRequest();
        $request->setVisitor($revokeVisitor);

        /**
         * @var OrderCancelResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 撤单接口
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return CancelOrderResponse
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): CancelOrderResponse
    {
        $request = new OrderCancelRequest();
        $request->setVisitor($recallVisitor);

        /*** @var CancelOrderResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): ElecPolicyDownloadResponse
    {
        $request = new ElecPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var ElecPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): OrderStatusQueryResponse
    {
        $request = new OrderStatusQueryRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var OrderStatusQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return CommonResponse|Response
     * @throws Exception
     */
    public function UploadFile(UploadFileVisitor $uploadFileVisitor)
    {

        $list = $uploadFileVisitor->fileList;
        if (empty($list)) {
            throw new Exception("图片不能为空");
        }
        $response = new CommonResponse();
        foreach ($list as $val) {
            $request = new UploadFileRequest();
            $visitor = new UploadFileVisitor();
            $visitor->argc = $uploadFileVisitor->argc;
            $visitor->ciInquiryNo = $uploadFileVisitor->ciInquiryNo;
            $visitor->biInquiryNo = $uploadFileVisitor->biInquiryNo;
            $visitor->fileAppend($val['type'], $val['url'], $val['base64']);
            $request->setVisitor($visitor);
            $response = $this->execute($request);
            usleep(500);
        }
        return $response;
    }


    /**
     * 非车产品列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     * @version 4.0
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);
        $response = new NonCarProductListResponse();
        $response->setCode(200);
        $response->seat = $nonCarProductListVisitor->seats;
        $response->staticList();
        if (!empty($nonCarProductListVisitor->argc['orderNo'])) {
            /**
             * @var NonCarProductListResponse $response
             */
            $response = $this->execute($request);
        }

        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    /**
     * 验车码接口
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        ///*** @var $response ValidateCarResponse */
        //$response = $this->execute(new ValidateCarRequest());

        $response = new ValidateCarResponse(200);
        $response->printerInit();
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
